using UnityEngine;

public class FreeCoinsButton : MonoBehaviour
{
	public UILabel coinText;

	private UISprite button;

	public int maxTimesToShowCollectCoins = 10;

	private bool isButtonActive;

	public GameObject arrow;

	public string activeButtonSpriteName = "Button.Affirmative";

	public string nonActiveButtonSpriteName = "Button.No";

	public Particle2D particles;

	public UISprite currencySprite;

	private void Awake()
	{
		button = GetComponent<UISprite>();
	}

	private void OnEnable()
	{
		UpdateButton();
	}

	public void OnClick()
	{
		TableTennisMainNavigation instance = TableTennisMainNavigation.instance;
		TableTennisCoinController coinControlForCurrency = instance.GetCoinControlForCurrency(CurrencyType.diamonds);
		if (GGPlayerSettings.instance.walletManager.isNextCoinsActive())
		{
			int amount = GGPlayerSettings.instance.walletManager.FreeCoins();
			GGPlayerSettings.instance.walletManager.CollectFreeCoins();
			if (coinControlForCurrency != null)
			{
				coinControlForCurrency.AnimateGetMoney(currencySprite, amount, null);
			}
			UpdateButton();
		}
		else
		{
			string @string = ScriptableObjectSingleton<SettingsUI>.instance.GetString(MultiplayerRooms.instance.multiplayerCurrencyType.ToString());
			TableTennisDialog.instance.ShowOk("Free " + @string + " Unavailable", "Free " + @string + " available again for collection in " + GGFormat.FormatTimeSpan(GGPlayerSettings.instance.walletManager.TimeTillNextCoinsActive()), "Ok", null);
		}
	}

	private void Update()
	{
		bool flag = GGPlayerSettings.instance.walletManager.isNextCoinsActive();
		if (flag != isButtonActive)
		{
			UpdateButton();
		}
		if (!flag)
		{
			SetTimeToButton();
		}
	}

	private void SetTimeToButton()
	{
		coinText.text = "Collect In:\n" + GGFormat.FormatTimeSpan(GGPlayerSettings.instance.walletManager.TimeTillNextCoinsActive());
	}

	private void UpdateButton()
	{
		isButtonActive = GGPlayerSettings.instance.walletManager.isNextCoinsActive();
		UITools.ChangeStyle(button, (!isButtonActive) ? nonActiveButtonSpriteName : activeButtonSpriteName);
		if (!isButtonActive)
		{
			SetTimeToButton();
			UITools.SetActive(arrow, false);
		}
		else
		{
			coinText.text = "Collect " + GGPlayerSettings.instance.walletManager.FreeCoins();
			UITools.SetActive(arrow, GGPlayerSettings.instance.Model.timesCollectedFreeCoins < maxTimesToShowCollectCoins);
		}
	}
}
